<!--
  speedy-vision.js
  GPU-accelerated Computer Vision for JavaScript
  Copyright 2020-2024 Alexandre Martins <alemartf(at)gmail.com>

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  nightvision-camera.html
  Nightvision demo: "see in the dark"
-->
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="description" content="speedy-vision.js: GPU-accelerated Computer Vision for JavaScript">
        <meta name="author" content="Alexandre Martins">
        <title>Nightvision camera</title>
        <link href="demo-base.css" rel="stylesheet">
        <script src="demo-base.js"></script>
        <script src="../dist/speedy-vision.js"></script>
    </head>
    <body>
        <h1>Nightvision camera</h1>
        <form autocomplete="off">
            <div>
                try in the dark!
            </div>
            <div class="separator"></div>
            <div>
                <label for="gain-slider">Gain</label>
                <input type="range" min="0" max="1" value="0.5" step="0.01" id="gain-slider">
                <label id="gain-viewer"></label>
            </div>
            <div>
                <label for="offset-slider">Offset</label>
                <input type="range" min="0" max="1" value="0.5" step="0.01" id="offset-slider">
                <label id="offset-viewer"></label>
            </div>
            <div>
                <label for="decay-slider">Decay</label>
                <input type="range" min="0" max="1" value="0" step="0.01" id="decay-slider">
                <label id="decay-viewer"></label>
            </div>
            <div class="separator"></div>
            <div>
                <label for="quality-select">Quality</label>
                <select id="quality-select">
                    <option value="high">high (best)</option>
                    <option value="medium" selected>medium</option>
                    <option value="low">low (fastest)</option>
                </select>
            </div>
            <div>
                <label for="greyscale-checkbox">Greyscale</label>
                <input type="checkbox" id="greyscale-checkbox" checked>
            </div>
            <div>
                <button type="reset">Reset</button>
            </div>
        </form>
        <div>
            <span id="status"></span>
            <canvas id="canvas-demo"></canvas>
        </div>
        <script>
window.onload = async function()
{
    // form controls
    const gainSlider = document.getElementById('gain-slider');
    const gainViewer = document.getElementById('gain-viewer');
    const offsetSlider = document.getElementById('offset-slider');
    const offsetViewer = document.getElementById('offset-viewer');
    const decaySlider = document.getElementById('decay-slider');
    const decayViewer = document.getElementById('decay-viewer');
    const qualitySelect = document.getElementById('quality-select');
    const greyscaleCheckbox = document.getElementById('greyscale-checkbox');

    setInterval(() => {
        gainViewer.innerText = Number(gainSlider.value).toFixed(2);
        offsetViewer.innerText = Number(offsetSlider.value).toFixed(2);
        decayViewer.innerText = Number(decaySlider.value).toFixed(2);
    }, 100);

    /*

    Our pipeline:

    Image  ---------------------> Image ---> Nightvision ---> Image
    Source                        Mux                         Sink
       |                           ^
       |         Convert to        |
       +-------> greyscale  -------+

    */

    // load the camera stream
    const media = await Speedy.camera();

    // create the pipeline
    const pipeline = Speedy.Pipeline();
    const source = Speedy.Image.Source();
    const sink = Speedy.Image.Sink();
    const mux = Speedy.Image.Multiplexer();
    const greyscale = Speedy.Filter.Greyscale();
    const nightvision = Speedy.Filter.Nightvision();

    source.media = media;

    source.output().connectTo(greyscale.input());
    source.output().connectTo(mux.input('in0'));
    greyscale.output().connectTo(mux.input('in1'));
    mux.output().connectTo(nightvision.input());
    nightvision.output().connectTo(sink.input());

    pipeline.init(source, sink, mux, greyscale, nightvision);

    // Main loop
    (function() {
        let image = null, frameReady = false;
        const canvas = setupCanvas('canvas-demo', media.width, media.height, 'Nightvision');
        canvas.style.width = (media.width * 2) + 'px';

        async function update()
        {
            mux.port = greyscaleCheckbox.checked ? 1 : 0;

            nightvision.gain = Number(gainSlider.value);
            nightvision.offset = Number(offsetSlider.value);
            nightvision.decay = Number(decaySlider.value);
            nightvision.quality = qualitySelect.value;

            const result = await pipeline.run();
            image = result.image;

            frameReady = true;
            setTimeout(update, 1000 / 60);
        }
        update();

        function render()
        {
            if(frameReady) {
                renderMedia(image, canvas);
                renderMedia(media, canvas, 0, 0, media.width / 3, media.height / 3);
            }

            frameReady = false;
            requestAnimationFrame(render);
        }
        render();

        setInterval(renderStatus, 200);
    })();
}
        </script>
        <section id="speedy-vision">
            <span>Powered by <a href="https://github.com/alemart/speedy-vision" target="_blank">speedy-vision.js</a></span>
            <a href="https://github.com/sponsors/alemart" target="_blank" class="button">
                <img alt="GitHub Sponsors" src="https://img.shields.io/github/sponsors/alemart?style=for-the-badge&logo=github&label=Sponsor&labelColor=royalblue&color=gold">
            </a>
        </section>
    </body>
</html>